/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.base.web.catalog;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.base.entity.catalog.BaseCatalog;
import com.thinkgem.jeesite.modules.base.service.catalog.BaseCatalogService;

/**
 * 基础管理_目录信息生成Controller
 * @author 呆子
 * @version 2020-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/base/catalog/baseCatalog")
public class BaseCatalogController extends BaseController {

	@Autowired
	private BaseCatalogService baseCatalogService;
	
	@ModelAttribute
	public BaseCatalog get(@RequestParam(required=false) String id) {
		BaseCatalog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseCatalogService.get(id);
		}
		if (entity == null){
			entity = new BaseCatalog();
		}
		return entity;
	}

	@RequiresPermissions("base:catalog:baseCatalog:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaseCatalog baseCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<BaseCatalog> list = baseCatalogService.findList(baseCatalog); 
		model.addAttribute("list", list);
		return "modules/base/catalog/baseCatalogList";
	}

	@RequiresPermissions("base:catalog:baseCatalog:view")
	@RequestMapping(value = "form")
	public String form(BaseCatalog baseCatalog, Model model) {
		if (baseCatalog.getParent()!=null && StringUtils.isNotBlank(baseCatalog.getParent().getId())){
			baseCatalog.setParent(baseCatalogService.get(baseCatalog.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(baseCatalog.getId())){
				BaseCatalog baseCatalogChild = new BaseCatalog();
				baseCatalogChild.setParent(new BaseCatalog(baseCatalog.getParent().getId()));
				List<BaseCatalog> list = baseCatalogService.findList(baseCatalog); 
				if (list.size() > 0){
					baseCatalog.setSort(list.get(list.size()-1).getSort());
					if (baseCatalog.getSort() != null){
						baseCatalog.setSort(baseCatalog.getSort() + 30);
					}
				}
			}
		}
		if (baseCatalog.getSort() == null){
			baseCatalog.setSort(30);
		}
		model.addAttribute("baseCatalog", baseCatalog);
		return "modules/base/catalog/baseCatalogForm";
	}

	@RequiresPermissions("base:catalog:baseCatalog:edit")
	@RequestMapping(value = "save")
	public String save(BaseCatalog baseCatalog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baseCatalog)){
			return form(baseCatalog, model);
		}
		baseCatalogService.save(baseCatalog);
		addMessage(redirectAttributes, "保存目录信息成功");
		return "redirect:"+Global.getAdminPath()+"/base/catalog/baseCatalog/?repage";
	}
	
	@RequiresPermissions("base:catalog:baseCatalog:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseCatalog baseCatalog, RedirectAttributes redirectAttributes) {
		baseCatalogService.delete(baseCatalog);
		addMessage(redirectAttributes, "删除目录信息成功");
		return "redirect:"+Global.getAdminPath()+"/base/catalog/baseCatalog/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<BaseCatalog> list = baseCatalogService.findList(new BaseCatalog());
		for (int i=0; i<list.size(); i++){
			BaseCatalog e = list.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("pId", e.getParentId());
            map.put("pIds", e.getParentIds());
            map.put("name", e.getName());
            map.put("pName",e.getParent().getName());
            mapList.add(map);
		}
		return mapList;
	}
	
}